package cn.longwingstech.intelligence.agent.mq.consumer;

import cn.longwingstech.intelligence.agent.mq.consumer.vo.EmailMqDTO;
import cn.longwingstech.intelligence.agent.utils.EmailSendUtil;
import com.alibaba.fastjson2.JSONObject;
import com.rabbitmq.client.Channel;
import jakarta.annotation.Resource;
import jakarta.mail.MessagingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * @author: shayu
 * @date: 2022年09月16日 14:52
 * @ClassName: DeadLetterQueueConsumer
 * @Description:	监听消息
 */
@Slf4j
@Component
public class DeadLetterQueueConsumer {

    @Resource
    private EmailSendUtil emailSendUtil;

    @RabbitListener(queues = "QD")
    public void receiveD(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        log.info("当前时间：{},收到死信队列信息{}", new Date().toString(), msg);
    }

    /**
     * 短信发送
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(queues = "Q_EMAIL")
    public void receiveQA(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        EmailMqDTO emailMqDTO = JSONObject.parseObject(msg, EmailMqDTO.class);
        try {
            emailSendUtil.sendEmail(emailMqDTO.getEmail(),emailMqDTO.getTitle(),emailMqDTO.getContent());
            log.info(emailMqDTO.toString());
            log.info("邮箱发送成功");
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}